package com.game.league.articles.adapter;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentActivity;

import com.game.base.app.base.BaseAdapter;
import com.game.base.glideV4.ImageLoader;
import com.game.base.utils.DecimalUtil;
import com.game.league.R;
import com.game.league.articles.table.Articles;
import com.game.league.databinding.AdapterArticlesBinding;
import com.game.league.util.SkipUtil;

/**
 * 物品
 * Created by Su on 2019/5/27.
 */
public class ArticlesAdapter extends BaseAdapter<Articles, AdapterArticlesBinding> {

    public ArticlesAdapter(Activity activity) {
        super(activity);
    }

    @Override
    protected int getLayoutResId(int viewType) {
        return R.layout.adapter_articles;
    }

    @Override
    protected void onBindItem(AdapterArticlesBinding binding, Articles item, int position) {
        ImageLoader.loadRoundCenterCrop(mActivity, item.getImgUrl(), binding.ivImage);

        String detailUrl = item.getDetailUrl();
        if (TextUtils.isEmpty(item.getDetailUrl())) {
            binding.tvDetail.setVisibility(View.GONE);
        } else {
            binding.tvDetail.setVisibility(View.VISIBLE);
            binding.ivImage.setOnClickListener(v -> SkipUtil.showWebActivity(mActivity, "详情", detailUrl, "", true));
        }

        binding.tvContent.setText(item.getTitle());
        binding.tvPrice.setText(DecimalUtil.getFormatFloat(item.getPrice()) + "点券");

        binding.btnExchange.setOnClickListener(v -> handleExchange(item));
    }

    private void handleExchange(Articles item) {
        SkipUtil.showExchangeDialog((FragmentActivity) mActivity, item);
    }
}
